<?php namespace Common\Model;
 use \Hdphp\Model\Model;
 /*
  * 文章模型
  * */
  class Article extends Model
  {
  		protected $ArticleData;//实例化data表
		protected $ArticleTag;//实例化中间表
  		/*
		 * 构造方法
		 * */
		 public function __init()
		 {
		 	$this->ArticleData = new \Common\Model\Article_Data;
			$this->ArticleTag = new\Common\Model\Article_Tag;
		 }
		 
  		protected $table = "article";//数据表
  		//自动验证
		protected $validate = array(
			array("title","required","标题不能为空",3,3),
			array("author","required","作者必须填写",3,3),
			array("keywords","required","关键字必须填写",3,3),
			array("digest","required","摘要必须填写",3,3),
		);
		//自动完成
		protected $auto =array(
			array("sendtime","time","function",3,1),//文章发表时间
			array("updatetime","time","function",3,2),//文章更新时间
			array("user_uid","useruid","method",3,1),//文章作者
			array("thumb","thumb_img","method",3,3),
		);
		//获得文章作者的方法
		protected function useruid(){
			return $_SESSION["uid"];
		}
		//获得缩略图
		protected function thumb_img(){
		//编辑时要判断是否有图片改动，如果没有改动路径是隐藏域中的value
			if(isset($_POST["thumb"])){
				return $_POST["thumb"];
			}
			$path = "Upload/" . date("ymd");
			$file = Upload::path($path)->type("jpg,png")->size(2000000)->make();
			//p($file);die;
			//判断是否传图，传图将图片进行缩略
			if($file){
				$thumbimg = Image::thumb($file[0]["path"],$path . "/thumb_" . $file[0]['basename'],200,200,5);
				return $thumbimg;
			}else{
				return "";
			}
		}
		/*
		 * 添加文章
		 * */
		public function add_Article()
		{
			
			//p($_POST);die;
			if(!$this->create()) return false;
			//自动验证其他表中的字段
			//1.先实例化这张表的模型
			$ArticleTag = new\Common\Model\Article_Tag;
			//2.执行自动验证
			if(!$ArticleTag->create()){
				$this->error = $ArticleTag->getError();
				return false;
			} 
			
			$ArticleData = new \Common\Model\Article_Data;
			if(!$ArticleData->create()){
				$this->error = $ArticleData->getError();
				return false;
			} 
			//添加数据
			$aid = $this->add();
			//添加文章数据表中的数据
			//1.实例化
			//处理数据
			$ArticleData->data["article_aid"] = $aid;
			//p($ArticleData->data);die;
			//执行添加
			$ArticleData->add();
			//添加文章中和标签中间表数据‘
			//处理数据
			foreach(Q("post.tag_tid",array()) as $v){
				$data = array(
					"article_aid" => $aid,
					"tag_tid" => $v,
				);
				$ArticleTag ->add($data);
			}
			
			return TRUE;
		}
		/*
		 * 将文章加入回收站
		 * */
		public function del(){
			$aid = Q("get.aid",0,"intval");
			//p($aid);die;
			
			//进入回收站
			$this->where("aid",$aid)->update(array("is_recycle"=>1));
			return true;
		}
		/*
		 *编辑
		 * */
		public function edit($aid){
			//p($aid);die;
			if(!$this->create()) return false;
			//中间表实例化
			if(!$this->ArticleTag->create()){
				$this->error = $this->ArticleTag->getError();
				return false;
			} 
			//验证数据表
			if(!$this->ArticleData->create()){
				$this->error = $this->ArticleData->getError();
				return false;
			} 
			$this->save();//执行文章表的修改
			//修改数据表
			$data = array(
				"keywords"=>$_POST["keywords"],
				"des"=>$_POST["des"],
				"content"=>$_POST["content"],
			);
			//p($data);die;
			$this->ArticleData->where("article_aid",$aid)->update($data);
			//修改中间表
			//1.先删除数据
			$this->ArticleTag->where("article_aid",$aid)->delete();
			//2.添加数据
			//处理数据
			foreach($_POST["tag_tid"] as $v){
				$data = array(
					"article_aid"=>$aid,
					"tag_tid"=>$v,
				);
				$this->ArticleTag->add($data);
			}
			return TRUE;
		}
		
		/*
		 * 回收站还原模型
		 * */
		public function reduction($aid)
		{
			//p($aid);die;
			//将is_recycle改为1
			$this->where("aid",$aid)->update(array("is_recycle"=>0));
			return true;
		}
		/*
		 *	回收站删除
		 * */
		public function Remove($aid)
		{
			$this->where("aid",$aid)->delete();
			//删数据表数据
			$this->ArticleData->where("article_aid",$aid)->delete();
			//删除中间表数据
			$this->ArticleTag->where("article_aid",$aid)->delete();
			return true;
		}
  }
	